package com.future;

import com.future.util.TreeNode;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 897. 递增顺序搜索树
 *
 * @author weiruibai.vendor
 * Date: 2023/1/6 09:36
 */
public class Solution_897 {

    private static Solution_897 instance = new Solution_897();

    public static void main(String[] args) {
        TreeNode root = new TreeNode(10);
        root.left = new TreeNode(5);
        root.right = new TreeNode(20);
        TreeNode treeNode = instance.increasingBST(root);
        System.out.println(treeNode);
    }

    public TreeNode increasingBST(TreeNode root) {
        TreeNode head = new TreeNode(-1);
        List<Integer> list = new ArrayList<>();
        buildIncreasingBST(root, list);
        TreeNode opNode = head;
        for (int i = 0; i < list.size(); i++) {
            opNode.right = new TreeNode(list.get(i));
            opNode = opNode.right;
        }
        return head.right;
    }

    private void buildIncreasingBST(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        buildIncreasingBST(root.left, list);
        list.add(root.val);
        buildIncreasingBST(root.right, list);
    }
}
